﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyLift.TMS.Web.Models;
using EasyLift.TMS.Web.Helpers;

namespace CoreBiz.Web.Controllers
{
    public class VehicleController : Controller
    {

        VehicleRepository repository = new VehicleRepository();
        //
        // GET: /Vehicle/

        //public ActionResult Index(int? page)
        //{
        //    int pageSize = 10;
        //    var Vehicles = repository.GetAllData();
        //    var paginatedVehicles = new PaginatedList<Vehicle>(Vehicles, page ?? 0, pageSize);
        //    return View("Index", paginatedVehicles);
        //}

        public ActionResult Index()
        {
            return View("Index");
        }

        public ActionResult Status()
        {
            return View("Status");
        }

        public JsonResult GridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
        {

            var allData = repository.GetAllData(sidx, sord, _search, searchField, searchOper, searchString);

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = allData.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var paginatedAllData = new PaginatedList<Vehicle>(allData, pageIndex, pageSize);


            var JsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from p in paginatedAllData
                    select new
                    {
                        id = p.VehicleNumber,
                        cell = new string[] {
                            p.VehicleNumber.ToString(),
                            p.DoorNo,
                            p.LicensePlateNumber,
                            p.CompanyCode,
                            p.VIN,
                            p.TrailorNo,
                            p.VehicleMissionstatus.VehicleMissionStatusDescription
                        }
                    }
                ).ToArray()
            };

            return Json(JsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GridDataByStatus(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString,string status)
        {

            var allData = repository.GetAllDataByStatus(sidx, sord, _search, searchField, searchOper, searchString, status);

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = allData.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var paginatedAllData = new PaginatedList<Vehicle>(allData, pageIndex, pageSize);


            var JsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from p in paginatedAllData
                    select new
                    {
                        id = p.VehicleNumber,
                        cell = new string[] {
                            p.VehicleNumber.ToString(),
                            p.DoorNo,
                            p.LicensePlateNumber,
                            p.CompanyCode,
                            p.VIN,
                            p.TrailorNo,
                            p.VehicleMissionstatus.VehicleMissionStatusDescription
                        }
                    }
                ).ToArray()
            };

            return Json(JsonData, JsonRequestBehavior.AllowGet);
        }


        //
        // GET: /Vehicle/Details/5

        public ActionResult Details(string id)
        {
            Vehicle Vehicle = repository.GetByID(id);
            if (Vehicle == null)
                return View("NotFound");
            else
                return View("Details", Vehicle);
        }

        //
        // GET: /Vehicle/Create

        public ActionResult Create()
        {
            Vehicle pf = new Vehicle();
            pf.VehicleNumber = "New Vehicle";
            pf.VehicleMissionStatusCode = "1";
            pf.VehicleQCStatusCode = "1";
            return View(new VehicleViewModel(pf));
        }

        //
        // POST: /Vehicle/Create

        [HttpPost]
        public ActionResult Create(Vehicle Vehicle)
        {

            //UpdateModel(Vehicle);
            try
            {
                if (String.IsNullOrEmpty(Vehicle.VehicleNumber))
                {
                    ModelState.AddModelError("VehicleNumber", "VehicleNumber is required");
                }

                if (ModelState.IsValid)
                {

                    repository.Add(Vehicle);
                    repository.Save();
                }
                else
                {
                    return View(new VehicleViewModel(Vehicle));
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(new VehicleViewModel(Vehicle));
            }

        }

        //
        // GET: /Vehicle/Edit/5

        public ActionResult Edit(string id)
        {
            try
            {
                Vehicle Vehicle = repository.GetByID(id);
                if (Vehicle == null)
                    return View("NotFound");

                return View(new VehicleViewModel(Vehicle));
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Vehicle/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, Vehicle Vehicle)
        {
            try
            {
                //UpdateModel(Vehicle);
                repository.Update(Vehicle);
                repository.Save();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DataVersionConflict", ex);

                return View(new VehicleViewModel(Vehicle));

            }

        }

        //
        // GET: /Vehicle/Delete/5

        public ActionResult Delete(string id)
        {
            Vehicle Vehicle = repository.GetByID(id);

            return View(Vehicle);
        }

        //
        // POST: /Vehicle/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                Vehicle Vehicle = repository.GetByID(id);

                repository.Delete(Vehicle);
                repository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

    }
}
